package com.chinasoft.mall.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.adapter.ModelListAdapter;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.ModellistBean;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.LoadListView;
import com.chinasoft.mall.widget.LoadListView.OnRefreshListener;

public class SyModelListActivity extends BaseActivity implements OnRefreshListener {

	private LoadListView modellist;
	private BaseBean modellist_bb;
	private ModelListAdapter mlAdapter;

	private BaseBean modelweibo_bb;
	private String modelweibotitle;
	private String modelweiboimageurl;
	private List<ModellistBean> mbs=new ArrayList<ModellistBean>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sy_more_modellist);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	private void initRequest() {
		/*
		 * modellist_bb =
		 * ControllerCenter.responsdata.get(Config.GET_MODELLIST); if
		 * (modellist_bb != null) { success(modellist_bb); } else
		 */{
			modellist_bb = new BaseBean(Config.GET_MODELLIST);
			modellist_bb.reqData.put("pageNo", "1");
			modellist_bb.reqData.put("pageSize", "10");
			control.sendMessage(modellist_bb);
		}
	}

	private void initView() {
		modellist = (LoadListView) findViewById(R.id.modellist);
		mlAdapter = new ModelListAdapter((MainActivity) this.getParent(), null);
		modellist.setAdapter(mlAdapter);
		modellist.setonRefreshListener(this);
		modellist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ModellistBean scd = (ModellistBean) mlAdapter.getItem(position);
				modelweiboimageurl = StringUtil.modelImageUrl(scd.getMODEL_ID(),scd.getTHUM());
				modelweibotitle = scd.getMODEL_NAME();
				modelweibo_bb = new BaseBean(Config.GET_MODELWEIBO);
				modelweibo_bb.reqData.put("pageNo", "1");
				modelweibo_bb.reqData.put("pageSize", "10");
				modelweibo_bb.reqData.put("model_id", scd.getMODEL_ID());
				control.sendMessage(modelweibo_bb);
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(modellist_bb)) {
			mbs.addAll((List<ModellistBean>) obj.getResData().getData());
			mlAdapter.setData(mbs);
			modellist.onRefreshComplete();
		} else if (obj.equals(modelweibo_bb)) {
			((MainActivity) this.getParent()).setCurrentActivity(
					SyModelList2Activity.class, modelweibotitle,
					modelweiboimageurl);
		}
	}
	@Override
	public void onRefresh() {
		modellist_bb.reqData.put("pageNo", (Integer.parseInt(modellist_bb.reqData.get("pageNo"))+1)+"");
		control.postMessage(modellist_bb);
	}
}
